package com.bigdata.utils;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class RandomOptionGroup<T> {
    int totalWeight=0;                       //总权重
    List<RanOpt> optList=new ArrayList();    //类型集合
    /**
     *
     * @param opts
     */
    public RandomOptionGroup(RanOpt<T>... opts) {
        for (RanOpt opt : opts) {
            totalWeight+=opt.getWeight();
            for (int i = 0; i <opt.getWeight() ; i++) {
                optList.add(opt);
            }
        }
    }

    /**
     *
     * @return
     */
    public RanOpt<T> getRandomOpt() {
        int i = new Random().nextInt(totalWeight);
        return optList.get(i);
    }

    public static void main(String[] args) {
        RanOpt[] opts= {
                new RanOpt("zhang3",20),
                new RanOpt("li4",30),
                new RanOpt("wang5",50)
        };
        RandomOptionGroup randomOptionGroup = new RandomOptionGroup(opts);
        int count = 0;
        for (int i = 0; i <100 ; i++) {
            if (count % 10 == 0){
                System.out.println();
            }
            Object obj = randomOptionGroup.getRandomOpt().getValue();
            System.out.print(obj + "\t");
            count++;
        }
    }
}
